package 华为OD机试真题2023.基础题;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class HJ03_查找重复代码 {
    public static void main(String[] args) throws FileNotFoundException {
        //方法一：暴力解，拿不到满分
//        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/基础题/input/03.txt"));
//        String s1 = in.nextLine();
//        String s2 = in.nextLine();
//        String res = "";
//        int cnt = Integer.MIN_VALUE;
//        if (s1.length() < s2.length()) {
//            for (int i = 0; i < s1.length(); i++) {
//                for (int j = i + 1; j < s1.length(); j++) {
//                    String s = s1.substring(i, j);
//                    if (s2.contains(s) && s.length() > cnt) {
//                        res = s;
//                        cnt = s.length();
//                    }
//                }
//            }
//        } else {
//            for (int i = 0; i < s2.length(); i++) {
//                for (int j = i + 1; j < s2.length(); j++) {
//                    String s = s2.substring(i, j);
//                    if (s1.contains(s) && s.length() > cnt) {
//                        res = s;
//                        cnt = s.length();
//                    }
//                }
//            }
//        }
//        System.out.println(res);

        //方法二：模式串下标0开始截取子串，1.若主串中有该子串，那么截取长度+1；2.若主串没有该子串，那么下标+1
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/基础题/input/03.txt"));
        String s1 = in.nextLine();
        String s2 = in.nextLine();
        String res = "";
        int start = 0;
        int end = 1;
        while (end <= s2.length()) {
            String subStr = s2.substring(start, end);
            if (s1.contains(subStr)) {
                if (subStr.length() > res.length()) {
                    res = subStr;
                }
            } else {
                start++;
            }
            end++;
        }
        System.out.println(res);
    }
}
